<?php
/**
 * Description of articles authors
 *
 * @author:	qfox
 * @package:	radiance
 * @copyright:	(c) 2011, alex <qfox@ya.ru> yaroshevich aka alex_ez. All rights reserved
 * @created:	11.05.2011, 14:34
 */
class articles_authors_admin_controller extends refbook_controller {

	protected $types = array();

	protected $entity_title = 'articles author';
	protected $entities_title = 'articles authors';
	protected $captions = null;
	protected $item_title_key = 'title';

	// form and filter
	protected $pager_per_pages = '20,50,200,500';

	protected $order = 'id-';

	protected $url_patterns = array(
		'index' => '/admin/articles/authors/',
		'show' => '/admin/articles/authors/id:id/',
		'add' => '/admin/articles/authors/add/',
		'edit' => '/admin/articles/authors/edit/?id=:id',
		);

	public function on_call( &$walker ) {
		if (!$this->conf['enabled'])
			respi()->notfound();
	}

	public function on_initialize( &$walker ) {
		$this->conf = $this->config->articles;
		$this->conf['enabled'] = isset($this->conf['enabled']) && $this->conf['enabled'];
		if (!$this->conf['enabled'])
			return;

		$this->model = new articles_authors();
		$this->types = $this->model->get_types();

		$this->columns = array(
			'id' => array(
				'text' => __('ID'),
				'width' => '30px',
				'order' => true,
				),
			'type' => array(
				'text' => __('Type'),
				'width' => '12%',
				'render' => true,
				),
			'title' => array(
				'text' => __('Title'),
				'order' => true,
				),
			'url' => array(
				'text' => __('URL'),
				'order' => true,
				'anchor' => array(
					'target' => '_blank',
					'title' => __('Open destination URL'),
					'href' => ':url',
					),
				),
			'articles' => array(
				'text' => __('Articles quantity'),
				'order' => '(select count(`id`) as `articles` from core_articles where `author_id` = `core_articles_authors`.`id`)+,`id`-',
				'value' => '(select count(`id`) as `articles` from core_articles where `author_id` = `core_articles_authors`.`id`)',
				'width' => '10%',
				),
			);

		$this->captions = array(
			'title'		=> __('Authors'),
			'index'		=> __('Articles authors'),

			'show'		=> __('Show author'),
			'add'		=> __('Create author'),
			'edit'		=> __('Modify author'),
			'remove'	=> __('Delete author'),

			'showing'	=> __('Author "%s"'),
			'adding'	=> __('New author'),
			'editing'	=> __('Modifing author "%s"'),
			'removing'	=> __('Deleting author "%s"'),
			);

		$this->fieldsets['index#filter'] = array(
			'id' => array( 'label' => __('ID'), 'type' => 'text', 'class' => 'shorttext' ),
			'type' => array( 'label' => __('Type'), 'type' => 'select', 'list' => ':types' ),
			'title' => array( 'label' => __('Title'), 'type' => 'text' ),
			);

		if (!$walker->selfonly)
			parent::on_initialize( $walker );
	}

	protected function init_lists() {
		$this->lists['types'] = $this->types;
		return parent::init_lists();
	}

	protected function init_crumbs() {
		$this->crumbs['/admin/articles/'] = __('Articles');
		return parent::init_crumbs();
	}

	public function render_field_type($k,$v) {
		return @$this->lists['types'][$v['type']];
	}

	
}


